package exercises;

/**
 * @author zerorain
 **/
import java.util.*;
//字符串转整数，先对+/-等符号做处理，然后直接判断是否是整数 转换就行 注意一下arr[i]是字符，要减去'0'
public class ex11 {
    public static int StrToInt(String str) {
        char[] arr = str.toCharArray();
        int out = 0;
        if(str == null || arr.length == 0 ){
            return 0;
        }
        int flg = 1;
        if(arr[0] == '+'){
            flg = 1;
            arr[0] = '0';
        }
        if(arr[0] == '-'){
            flg = -1;
            arr[0] = '0';
        }
        for(int i = 0 ; i < arr.length; i++){
            if(arr[i] < '0' || arr[i] > '9'){
                return 0;
            }
            out = out*10 + (arr[i]-'0');
        }
        return flg*out;
    }

}